package cqrtplm.controller.thirdPartyAPI;

import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.SAPSupplierDTO;
import cqrtplm.dto.thirdPartyAPI.*;
import cqrtplm.service.SapApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "SAP")
@RequestMapping("/rest"+RouteConstant.RATO + "sap/" + "v1")
public class SAPAPIController {
    @Resource
    private SapApiService sapApiService;

    @GetMapping("/getSAPSupplier")
    @ApiOperation(value = "getSAPSupplier", notes = "根据图号获取SAP供应商")
    @ResponseBody
    public List<SAPSupplierDTO> getSAPSupplier(@RequestParam String factoryId,
                                               @RequestParam String drawingCode) {
        // 分割工厂ID
        String[] factoryIds = factoryId.split(",");
        // 用于存储所有结果的Map（避免重复）
        Map<String, SAPSupplierDTO> resultMap = new LinkedHashMap<>();

        // 遍历每个工厂ID，调用API并合并结果
        for (String fid : factoryIds) {
            Map<String, SAPSupplierDTO> supplierMap = sapApiService.getSAPSupplier(fid, drawingCode);
            resultMap.putAll(supplierMap);
        }

        return new ArrayList<>(resultMap.values());
    }

    /**
     * sap物料主数据导入接口
     * @param dto
     * @return
     */
    @PostMapping("/restAdapterWl")
    public RestAdapterWlDTO restAdapterWl(@RequestBody RestAdapterWlDTO dto) {
        return sapApiService.restAdapterWl(dto);
    }

    /**
     * sapBOM数据导入接口
     * @param dto
     * @return
     */
    @PostMapping("/restAdapterBom")
    public RestAdapterBomDTO restAdapterBom(@RequestBody RestAdapterBomDTO dto) {
        return sapApiService.restAdapterBom(dto);
    }

    @PostMapping("/restAdapter/qas/")
    public RestAdapterEsReturDTO restAdapterQas(@RequestBody RestAdapterQasDTO dto) {
        return sapApiService.restAdapterQas(dto);
    }

    @PostMapping("/getSuppliersByMaterial")
    public SapRespAdapterSreqDTO getSuppliersByMaterial(@RequestBody SapRestAdapterSreqDTO.InTab inTab) {
        return sapApiService.getSuppliersByMaterial(inTab);
    }


}
